package com.sky.myshop.services;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.HashMap;
import java.util.Map;

//实现spring的动态载入
public class ShapeSeriesFactory implements ApplicationContextAware {
    private static Map<ShapeEnum,ShapeSeries> seriesMap;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        //从Spring容器中搜索到所有的ShapeSeries的实现类
        Map<String, ShapeSeries> allSon = context.getBeansOfType(ShapeSeries.class);
        seriesMap = new HashMap<ShapeEnum,ShapeSeries>();
        //将所有的对象都存放到seriesMap中并给个枚举作为key
        allSon.forEach((k,v)->seriesMap.put(v.getShape(),v));
    }

    public static <T extends ShapeSeries> T getShapeDataMode(ShapeEnum se){
        return (T)seriesMap.get(se);
    }
}
